package edu.purdue.spm.component;

import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import edu.purdue.spm.GroupDashBoardFragment;
import edu.purdue.spm.Login;
import edu.purdue.spm.R;
import edu.purdue.spm.util.Portal;

public class GroupTaskInformationPanelFragment extends Fragment{

	static View base;
	boolean isleader = false;
	String task_info = "";
	
	String task_id = "";
	String parent_task_id = "";
	String owner_id = "";
	String task_title = "";
	String task_content = "";
	String due_time = "";
	String progress = "";
	String type = "";
	String weight = "";
	String depth = "";
	String assign_time = "";
	
	
	EditText task_title_textedit;
	TextView task_owner_textview;
	TextView assign_date_textview;
	TextView due_time_textview;
	TextView progress_textview;
	SeekBar progress_seekBar;
	EditText task_detail_textedit;
	Button modify;
	Button delete;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
		Log.i("MSG", "TaskInformationPanelFragment onCreateView !!!");
		base = inflater.inflate(R.layout.group_task_detail_information_panel, container, false);
		
		task_title_textedit = (EditText) base.findViewById(R.id.tasktitle_EditText);
		task_owner_textview = (TextView) base.findViewById(R.id.taskowner_textview);
		assign_date_textview = (TextView) base.findViewById(R.id.dateassigned_textview);
		due_time_textview = (TextView) base.findViewById(R.id.duetime_textview);
		progress_textview = (TextView) base.findViewById(R.id.progress_textView);
		progress_seekBar = (SeekBar) base.findViewById(R.id.progress_seekBar1);
		task_detail_textedit = (EditText) base.findViewById(R.id.taskcontent_detail_EditText);
		modify = (Button) base.findViewById(R.id.modify_task_button);
		delete = (Button) base.findViewById(R.id.delete_task_button);

		setSeekBarListener();
		
		populate();
		
		setModifyButtonListener();
		setDeleteButtonListener();

        return base;
    }
	
	private void setSeekBarListener() {
		progress_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		    }       

		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		    }       

		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
		    	progress_textview.setText(Integer.toString(progress) + "%");
		    }       
		});    
		
	}

	public void initailize(boolean isleader, String task_info){
		this.isleader = isleader;
		this.task_info = task_info;
		
		try{
			populate();
		} catch (Exception e) {
			
		}
	}
	
	public void populate() {
		try{
			String temp[] = task_info.split(":;:");
			 task_id = temp[0];
			 parent_task_id = temp[1];
			 owner_id = temp[2];
			 task_title = temp[3];
			 task_content = temp[4];
			 due_time = temp[5];
			 progress = temp[6];
			 type = temp[7];
			 weight = temp[8];
			 depth = temp[9];
			 assign_time = temp[10];
			 
			 task_title_textedit.setText(task_title);
			 task_owner_textview.setText(owner_id);
			 assign_date_textview.setText(assign_time);
			 due_time_textview.setText(due_time);
			 progress_seekBar.setProgress(Integer.parseInt(progress));
			 //progress_textedit.setText(progress);
			 task_detail_textedit.setText(task_content);
			 
			 if(!isleader) {
				 delete.setVisibility(View.INVISIBLE);
				 task_title_textedit.setKeyListener(null);
				 task_detail_textedit.setKeyListener(null);
			 }
			 
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void setModifyButtonListener(){
		modify.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				deleteTask(Login.GCMaccount, task_id);
				
				task_title = task_title_textedit.getText().toString();
				task_content = task_detail_textedit.getText().toString();
				//progress_seekBar.getProgress();
				progress = Integer.toString(progress_seekBar.getProgress());
				//progress = progress.substring(0, progress.length()-1);
				String group_id = GroupDashBoardFragment.group_ID[GroupDashBoardFragment.currentSelectedGroup];
				assignNewTask(owner_id, task_title, task_content, due_time, progress, weight, depth, group_id);
			}
		});
	}
	
	public void setDeleteButtonListener(){
		delete.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setMessage("Are you sure to delete this task?")
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						//delete task
						deleteTask(Login.GCMaccount, task_id);
						
						//update task list
						GroupDashBoardFragment.getGroupTaskIDs(GroupDashBoardFragment.group_ID[GroupDashBoardFragment.currentSelectedGroup]);
						GroupDashBoardFragment.getGroupTaskInfos(GroupDashBoardFragment.group_task_IDs);
						GroupDashBoardFragment.getTaskNames(GroupDashBoardFragment.group_task);
						setTaskList(GroupDashBoardFragment.group_task_name);
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					}
				});
				builder.create();
				builder.show();
			}
		});
	}
	
	public void deleteTask(String user_id, String tid){
		try {
			Portal.message = "DeleteTask:;:"+user_id+":;:"+tid;
			Portal.sendMessage(Portal.message);
			
			Portal.ReadMessages tmp = new Portal.ReadMessages();
			tmp.execute().get();

		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
	
	private void setTaskList(String [] tasks) {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.group_task_listview, tasks);
		GroupDashBoardFragment.taskList.setAdapter(adapter); 
	}

	private void assignNewTask(String owner_id, String task_title, String task_content,
			String due_time,String progress, String task_weight, String depth, String group_id){

		System.out.println("Assigning new task.");
		try {
			String task_id = group_id+System.currentTimeMillis()/1000;
			Portal.message = "AddGroupTask:;:"+Login.GCMaccount+":;:"+
					task_id+":.:0:.:'"+owner_id+"':.:'"+task_title+
					"':.:'"+task_content+"':.:'"+due_time+"':.:"+progress+
					":.:'G':.:"+task_weight+":.:'"+depth+"':.:"+group_id;

			Portal.sendMessage(Portal.message);
			Portal.ReadMessages tmp = new Portal.ReadMessages();
			tmp.execute().get();

			Toast.makeText(base.getContext(), Portal.messageBack, Toast.LENGTH_LONG).show();

			GroupDashBoardFragment.getGroupTaskIDs(GroupDashBoardFragment.group_ID[GroupDashBoardFragment.currentSelectedGroup]);
			GroupDashBoardFragment.getGroupTaskInfos(GroupDashBoardFragment.group_task_IDs);
			GroupDashBoardFragment.getTaskNames(GroupDashBoardFragment.group_task);
			setTaskList(GroupDashBoardFragment.group_task_name);
			
		} catch (InterruptedException e) {

			e.printStackTrace();
		} catch (ExecutionException e) {

			e.printStackTrace();
		}

	}
}
